import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import LoginPage from '@/views/LoginPage.vue'
import Dashboard from '@/views/Dashboard.vue'
import KnowledgeBaseManagement from '@/views/KnowledgeBaseManagement.vue'
import KnowledgeBase from '@/views/KnowledgeBase.vue'
import AIAssistant from '@/views/AIAssistant.vue'
import ApiPlatform from '@/views/ApiPlatform.vue'
import ChatWindow from '@/views/ChatWindow.vue'
import MCPMarket from '@/views/MCPMarket.vue'
import ApiDocs from '@/views/ApiDocs.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginPage,
    meta: {
      requiresAuth: false,
      title: '登录 - 店小二Agent'
    }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Dashboard,
    meta: {
      requiresAuth: true,
      title: '店小二Agent'
    },
    children: [
      {
        path: '',
        redirect: 'ai-assistant'
      },
      {
        path: 'knowledge-bases',
        name: 'KnowledgeBaseManagement',
        component: KnowledgeBaseManagement,
        meta: {
          requiresAuth: true,
          title: '知识库管理 - 店小二Agent'
        }
      },
      {
        path: 'knowledge-bases/:knowledgeBaseId/documents',
        name: 'knowledge-base-documents',
        component: KnowledgeBase,
        props: true,
        meta: {
          requiresAuth: true,
          title: '知识库文档管理 - 店小二Agent'
        }
      },
      {
        path: 'chat/:id?',
        name: 'ChatWindow',
        component: ChatWindow,
        props: true,
        meta: {
          requiresAuth: true,
          title: '聊天预览 - 店小二Agent'
        }
      },
      {
        path: 'ai-assistant',
        name: 'AIAssistant',
        component: AIAssistant,
        meta: {
          requiresAuth: true,
          title: '创建智能客服 - 店小二Agent'
        }
      },
      {
        path: 'api-platform',
        name: 'ApiPlatform',
        component: ApiPlatform,
        meta: {
          requiresAuth: true,
          title: 'API开放平台 - 店小二Agent'
        }
      },
      {
        path: 'mcp-market',
        name: 'MCPMarket',
        component: MCPMarket,
        meta: {
          requiresAuth: true,
          title: 'MCP市场 - 店小二Agent'
        }
      }
    ]
  },
  {
    path: '/api-docs',
    name: 'ApiDocs',
    component: ApiDocs,
    meta: {
      requiresAuth: false,
      title: 'API文档 - 店小二Agent'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 动态设置页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = '店小二Agent'
  }

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
    
    if (!isLoggedIn) {
      ElMessage.warning('请先登录')
      next('/login')
      return
    }
  }

  // 如果已登录用户访问登录页面，重定向到主页
  if (to.path === '/login') {
    const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
    if (isLoggedIn) {
      next('/dashboard/ai-assistant')
      return
    }
  }

  next()
})

// 路由后置守卫
router.afterEach((to, from) => {
  // 可以在这里添加页面访问统计等逻辑
  console.log(`路由跳转: ${from.path} -> ${to.path}`)
})

export default router 